<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class  MY_Controller  extends  CI_Controller  {
    var $seizoen;
    var $user;
    var $url;
    
    function MY_Controller ()  {
        parent::__construct();
        $this->load->model('ploegmodel');
                $this->load->model('seizoenmodel');
                $seizoenen = $this->seizoenmodel->SelectActive();
                $seizoenen = $seizoenen[0];
                $this->seizoen = $seizoenen;
                if(false !== $url = $this->session->flashdata('url') or false !== $url = $this->input->get('url')){
                    $this->url = $url;
                    
                }
                else {
                    if($this->agent->is_referral()){
                            $this->url = $this->agent->referrer();
                    } else {
                        $this->url = base_url(); 
                    }
                }
    }
    
    function IsAdmin(){
        if($this->session->userdata('admin') == 1){
            return  true;
        }
        else{
            return false;
        }
    }
    
    function GoBack($anchor = null){
        if($anchor == null){
            $anchor = "";
        }
        

        redirect($this->url.'#'.$anchor);
        
    }
    
    function GetUser(){
        
         $this->load->model('spelermodel');
         if(!isset($this->user)){
            If(false !== $this->session->userdata('user')){
                $speler = $this->spelermodel->SelectById($this->session->userdata('user'),$this->seizoen->Id);
                //die(print_r($speler));
                $this->user = $speler[0];
            }
            else return null;
         }
         
         return $this->user;
         
        
        
    }
    
    function Error($message){
        $this->SetMessage($message);
        $this->GoBack();
    }
    
    function LoadView($page,$data){
        //die("$page $data");
        //$viewdata = array();
        $viewdata = $data;
        if(false !== $this->session->flashdata('message')){
            $viewdata['message'] = $this->session->flashdata('message');
        }
        else{
            $viewdata['message'] = null;
        }
        $viewdata['admin'] = $this->IsAdmin();
        if(null !== $this->GetUser()){
            $viewdata['user'] = $this->GetUser();
        }
        
        $viewdata['page'] = $page;
        $this->load->view('page/template',$viewdata);
    }
    
    function SetMessage($message){
        $this->session->set_flashdata('message',$message);
    }
    
    function GetUpdate(){
       
        if(false !== $this->input->post('update')){
            return $this->input->post('update');
        }
        if(false !== $this->session->flashdata('update')){
            return $this->session->flashdata('update');
        }
        
        return false;
    }
    
    function SetUpdate($id){
        
        $this->session->set_flashdata('update',$id);
        
    }
    
     private function VerifyInput($regex,$value){
         
        //die("$regex $value".print_r(preg_match($regex,$value)));
        if(preg_match($regex,$value)){
            return true;
        } else {
            //die("$regex $value");
            return false;
        }
        
    }
    
    function VerifyEmail($string){
        $regex = '/[A-Za-z0-9._%+\-]+@[A-Za-z0-9\.\-]+\.[A-Za-z]{2,4}/';
        
        if(is_array($string)){
            foreach($string as $i){
                if($this->VerifyInput($regex, $i)){
                    
                } else return false;
                
            }
            
            return true;
        } else return $this->VerifyInput ($regex, $string);
            
        }
        
    
    
    function VerifyDate($string) {
        $regex = "/(0[1-9]|[12][0-9]|3[01])[\-\/.](0[1-9]|1[012])[\-\/.](19|20)\d\d/";
       
        if(is_array($string)){
            foreach($string as $i){
                if($this->VerifyInput($regex, $i)){
                    
                } else return false;
                
            }
            
            return true;
        } else return $this->VerifyInput ($regex, $string);
            
        }
       
        
        
        
    function VerifyNumber($string){
        $regex = "//";
       
        if(is_array($string)){
            foreach($string as $i){
                if($this->VerifyInput($regex, $i)){
                    
                } else return false;
                
            }
            
            return true;
        } else return $this->VerifyInput ($regex, $string);
            
        }
   
    
    
    function VerifyString($string){
       // die(print_r($string));
        $regex = "/[a-zA-Z ]+/";
        if(is_array($string)){
            foreach($string as $i){
                if($this->VerifyInput($regex, $i)){
                    
                } else return false;
                
            }
            
            return true;
        } else return $this->VerifyInput ($regex, $string);
            
        }
        
}
    
 ?>
       
        
   
    

